﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class EditModelName : BasePage
    {
        RepairModels model;
        int modelID;
        int brandID;
        string mode;
        const string BRAND_ID = "BrandID";

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if(Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            model = (RepairModels)Session[Request.QueryString[KEY]];
            string oldFilter = model.Filter;

            modelID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
            {
                model.Filter = RepairModels.ColumnNames.ID + "=" + modelID.ToString();
            }
            else
            {
                if (Request.QueryString[BRAND_ID] != null)
                    brandID = int.Parse(Request.QueryString[BRAND_ID]);
                else
                {
                    CloseDlg(0);
                    return;
                }
                btnSave.Enabled = false;
            }

            if (!IsPostBack && model.RowCount > 0 && mode != ADD_MODE)
                editModel.ModelName = model.ModelName;

            model.Filter = oldFilter;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if (model != null)
            {
                Save();
            }
        }

        private bool Save()
        {
             TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
             string oldFilter = model.Filter;
             try
             {
                 tx.BeginTransaction();
                 if (mode == ADD_MODE)
                 {
                     model.AddNew();
                     model.BrandID = brandID;
                     model.Deleted = false;
                     model.BranchID = Branches.CurrentBranchID;
                     if (RepairModels.IsModelNameExists(editModel.ModelName))
                     {
                         model.MarkAsDeleted();
                         throw new Exception(string.Format("Model {0} already exists!", editModel.ModelName));
                     }
                 }
                 else
                 {
                     model.Filter = RepairModels.ColumnNames.ModelName + " LIKE '" + editModel.ModelName + "' AND " + RepairModels.ColumnNames.ID + " <> " + modelID;
                     if (model.RowCount > 0)
                         throw new Exception(string.Format("Model {0} already exists!", editModel.ModelName));

                     model.Filter = RepairModels.ColumnNames.ID + "=" + modelID;
                 }
                 model.ModelName = editModel.ModelName;
                 model.LastModifiedByUser = SessionUser.ID;
                 model.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);

                 model.Save();

                 if (mode == ADD_MODE)
                     RepairModelProblems.InitForNewModel(model.ID);

                 ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
                 model.Filter = oldFilter;
                
                 tx.CommitTransaction();
                 ((Popup)Master).ShowMessage("Saved");
                 return true;
             }
             catch (Exception ex)
             {
                 tx.RollbackTransaction();
                 model.Filter = oldFilter;
                 ErrorLog.WriteToFile("EditModelName.aspx.cs", "Save", ex.StackTrace);
                 ((Popup)Master).ShowErrorMessage("Error:" + ex.Message);
                 return false;
             }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (model != null)
            {
                if(Save())
                    CloseDlg(1);
            }
        }
    }
}
